package com.qf.java2007.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.qf.java2007.mapper.BookInfoMapper;
import com.qf.java2007.mapper.ClassInfoMapper;
import com.qf.java2007.pojo.BookInfo;
import com.qf.java2007.pojo.ClassInfo;
import com.qf.java2007.service.BookInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author AzureSky_X
 * @Date 2021/3/8 17:46
 * @Version 1.0
 */
@Service
@Transactional
public class BookInfoServiceImpl implements BookInfoService {
    @Autowired
    private BookInfoMapper bookInfoMapper;
    @Autowired
    private ClassInfoMapper classInfoMapper;

    @Override
    public List<BookInfo> findAll() {
        return bookInfoMapper.findAll();
    }

    @Override
    public BookInfo findByIdWithClass(Long bookId) {
        return bookInfoMapper.findByIdWithClass(bookId);
    }

    @Override
    public List<BookInfo> findByNameOrAuthor(String searchWord) {
        //必须保证用户输入的值不为空，否则SQL语句会报错
        //判断用户输入的值是否为空，若为空则给变量searchWord赋值为空字符串
        if (StringUtils.isEmpty(searchWord)){
            searchWord="";
        }
        //模糊查询
        searchWord="%"+searchWord.trim()+"%";
        return bookInfoMapper.findByNameOrAuthor(searchWord);
    }

    @Override
    public void delete(Long bookId) {
        bookInfoMapper.delete(bookId);

    }

    @Override
    public void add(BookInfo bookInfo) {
        bookInfoMapper.add(bookInfo);
    }

    @Override
    public void update(BookInfo bookInfo) {
        bookInfoMapper.update(bookInfo);
    }

    @Override
    public Map<String, Object> findById(Long bookId) {
        BookInfo bookInfo=bookInfoMapper.findByIdWithClass(bookId);

        List<ClassInfo> list=classInfoMapper.findAll();
        Map<String, Object> map=new HashMap<>();
        map.put("bookInfo",bookInfo);
        map.put("classInfo",list);
        return map;
    }




}
